<template>
  <div class="comprehensive-table-container">
    <vab-query-form>
      <vab-query-form-top-panel>
        <el-form inline label-width="80px" :model="queryForm" @submit.prevent>
          <el-form-item label="员工姓名">
            <el-input v-model="queryForm.name" clearable placeholder="请输入标题" />
          </el-form-item>
          <el-form-item label="部门身份">
            <el-select v-model="queryForm.departmentIdentity" placeholder="请选择">
              <el-option v-for="item in identityEnum" :key="item.id" :label="item.label" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="工时填写">
            <el-select v-model="queryForm.need" placeholder="请选择">
              <el-option v-for="item in needWorkEnum" :key="item.id" :label="item.label" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" :loading="listLoading" native-type="submit" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-top-panel>
      <vab-query-form-left-panel>
        <el-button :icon="Plus" type="primary" @click="handleAdd">添加</el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table ref="tableRef" v-loading="listLoading" border :data="list">
      <el-table-column align="center" label="序号" width="55">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名" min-width="200" prop="loginName" show-overflow-tooltip />
      <el-table-column align="center" label="邮箱" min-width="200" prop="email" show-overflow-tooltip />
      <el-table-column align="center" label="角色" min-width="100" prop="roleName" show-overflow-tooltip />
      <el-table-column align="center" label="中文名" min-width="130" prop="accountName" show-overflow-tooltip />
      <el-table-column align="center" label="英文名" min-width="130" prop="enName" show-overflow-tooltip />
      <el-table-column align="center" label="性别" prop="sex">
        <template #default="{ row }">
          {{ row.sex == 1 ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="地址" prop="address" show-overflow-tooltip />
      <el-table-column align="center" label="部门" min-width="200" prop="departmentName" show-overflow-tooltip />
      <el-table-column align="center" label="部门身份" min-width="100" prop="departmentIdentity" />
      <el-table-column align="center" label="状态" min-width="100">
        <template #default="{ row }">
          <el-tag effect="dark" :type="statusFilter(row.state)">
            {{ row.state == 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button :disabled="row.state != 1" text type="primary" @click="handleState(row)">禁用</el-button>
          <el-button text type="primary" @click="handleEdit(row)">编辑</el-button>
          <!-- <el-button text type="danger" @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <vab-pagination
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script lang="ts" setup>
import { Plus, Search } from '@element-plus/icons-vue'
import Edit from './components/Edit.vue'
import { getList, setState } from '/@/api/account'
import { identityEnum, needWorkEnum } from '/@/utils/EnumUtil'
defineOptions({
  name: 'DefaultTable',
})

const $baseMessage = inject<any>('$baseMessage')
const editRef = ref<any>(null)
const tableRef = ref<any>(null)
const list = ref<any>([])
const listLoading = ref<boolean>(true)
const total = ref<any>(0)
const queryForm = reactive<any>({
  pageNo: 1,
  pageSize: 20,
})

const fetchData = async () => {
  listLoading.value = true
  const { data } = await getList(queryForm)
  list.value = data.list
  total.value = data.total
  listLoading.value = false
}

const handleSizeChange = (value: number) => {
  queryForm.pageNo = 1
  queryForm.pageSize = value
  fetchData()
}

const handleCurrentChange = (value: number) => {
  queryForm.pageNo = value
  fetchData()
}

const queryData = () => {
  queryForm.pageNo = 1
  fetchData()
}

const statusFilter = (status: string | number) => {
  const statusMap: any = {
    '1': 'success',
    '2': 'danger',
  }
  return statusMap[status]
}

const handleAdd = () => {
  editRef.value.showEdit()
}

const handleEdit = (row = {}) => {
  editRef.value.showEdit(row)
}

const handleState = async (row: any) => {
  const { code, message }: any = await setState({ accountId: row.accountId, state: 0 })
  $baseMessage(message, 'success', 'hey')
  if (code == 200) {
    fetchData()
  }
}

onActivated(() => {
  tableRef.value.doLayout()
})

onBeforeMount(() => {
  fetchData()
})
</script>
